What is vscode-json-languageservice?
The vscode-json-languageservice npm package provides a set of services for working with JSON documents in a way that is compatible with the Visual Studio Code editor. It offers features such as validation, completion, hover information, and more, making it easier to build tools and extensions that work with JSON data.
What are vscode-json-languageservice's main functionalities?
Validation
This feature allows you to validate JSON documents. The code sample demonstrates how to use the language service to validate a JSON document and print out any diagnostics (errors or warnings) found.
const { getLanguageService } = require('vscode-json-languageservice');
const languageService = getLanguageService({});
const document = { uri: 'file://test.json', languageId: 'json', version: 1, getText: () => '{ "key": "value" }' };
const jsonDocument = languageService.parseJSONDocument(document);
languageService.doValidation(document, jsonDocument).then(diagnostics => {
console.log(diagnostics);
});
Completion
This feature provides auto-completion suggestions for JSON documents. The code sample shows how to get completion suggestions at a specific position in a JSON document.
const { getLanguageService } = require('vscode-json-languageservice');
const languageService = getLanguageService({});
const document = { uri: 'file://test.json', languageId: 'json', version: 1, getText: () => '{ "key": "value" }' };
const jsonDocument = languageService.parseJSONDocument(document);
languageService.doComplete(document, { line: 0, character: 1 }, jsonDocument).then(completionList => {
console.log(completionList);
});
Hover Information
This feature provides hover information for JSON documents. The code sample demonstrates how to get hover information at a specific position in a JSON document.
const { getLanguageService } = require('vscode-json-languageservice');
const languageService = getLanguageService({});
const document = { uri: 'file://test.json', languageId: 'json', version: 1, getText: () => '{ "key": "value" }' };
const jsonDocument = languageService.parseJSONDocument(document);
languageService.doHover(document, { line: 0, character: 1 }, jsonDocument).then(hover => {
console.log(hover);
});
Other packages similar to vscode-json-languageservice
jsonlint
jsonlint is a JSON parser and validator with a CLI. It is primarily used for validating JSON data and providing error messages. Unlike vscode-json-languageservice, it does not offer features like completion or hover information.
ajv
ajv is a JSON schema validator that supports JSON Schema draft-07 and other standards. It is highly performant and can be used for validating JSON data against schemas. While it focuses on validation, it does not provide language service features like completion or hover information.
json-schema-to-typescript
json-schema-to-typescript converts JSON schemas to TypeScript interfaces. It is useful for generating TypeScript types from JSON schemas but does not offer the comprehensive language services provided by vscode-json-languageservice.
vscode-json-languageservice
JSON language service extracted from VSCode to be reused, e.g in the Monaco editor.
Why?
The vscode-json-languageservice contains the language smarts behind the JSON editing experience of Visual Studio Code
and the Monaco editor.
- doValidation analyses an input string and returns syntax and lint errros.
- doComplete provides completion proposals for a given location.
- doResolve resolves a completion proposals.
- doHover provides a hover text for a given location.
- findDocumentSymbols provides all symbols in the given document
- format formats the code at the given range.
Installation
npm install --save vscode-json-languageservice